home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / arpdump.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  3KB  |  124 lines

  1. /* ARP packet tracing routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include <stdio.h>
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "arp.h"
  8. #include "netuser.h"
  9. #include "trace.h"
  10.  
  11. void
  12. #ifdef MONITOR
  13. arp_dump(fp,bpp,mon)
  14. int mon;
  15. #else
  16. arp_dump(fp,bpp)
  17. #endif
  18. FILE *fp;
  19. struct mbuf **bpp;
  20. {
  21.     struct arp arp;
  22.     struct arp_type *at;
  23.     int is_ip = 0;
  24.     char tmp[25];
  25.  
  26.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  27.         return;
  28. #ifdef MONITOR
  29.     if (mon)
  30.     {
  31.         /* it's different enough that we just do it here */
  32.         /* except for the setup, that is... */
  33.         if (ntoharp(&arp,bpp) == -1)
  34.         {
  35.             fprintf(fp, "bad packet\n");
  36.             return;
  37.         }
  38.         if (arp.hardware < NHWTYPES)
  39.             at = &Arp_type[arp.hardware];
  40.         else
  41.             at = NULLATYPE;
  42.         if (at != NULLATYPE && arp.protocol == at->iptype)
  43.             is_ip = 1;
  44.         switch (arp.opcode)
  45.         {
  46.         case ARP_REQUEST:
  47.         case REVARP_REQUEST:
  48.             fprintf(fp, "REQ");
  49.             break;
  50.         case ARP_REPLY:
  51.         case REVARP_REPLY:
  52.             fprintf(fp, "RESP");
  53.             break;
  54.         default:
  55.             fprintf(fp, "%u", arp.opcode);
  56.             break;
  57.         }
  58.         if (is_ip)
  59.             fprintf(fp, " %s@", inet_ntoa(arp.sprotaddr));
  60.         if (at)
  61.             fprintf(fp, "%s->", at->format(tmp, arp.shwaddr));
  62.         if (is_ip)
  63.             fprintf(fp, "%s@", inet_ntoa(arp.tprotaddr));
  64.         if (at)
  65.             fprintf(fp, "%s", at->format(tmp, arp.thwaddr));
  66.         fprintf(fp, "\n");
  67.         return;
  68.     }
  69. #endif
  70.     fprintf(fp,"ARP: len %d",len_p(*bpp));
  71.     if(ntoharp(&arp,bpp) == -1){
  72.         fprintf(fp," bad packet\n");
  73.         return;
  74.     }
  75.     if(arp.hardware < NHWTYPES)
  76.         at = &Arp_type[arp.hardware];
  77.     else
  78.         at = NULLATYPE;
  79.  
  80.     /* Print hardware type in Ascii if known, numerically if not */
  81.     fprintf(fp," hwtype %s",smsg(Arptypes,NHWTYPES,(unsigned)arp.hardware));
  82.  
  83.     /* Print hardware length only if unknown type, or if it doesn't match
  84.      * the length in the known types table
  85.      */
  86.     if(at == NULLATYPE || (int16)arp.hwalen != at->hwalen)
  87.         fprintf(fp," hwlen %u",arp.hwalen);
  88.  
  89.     /* Check for most common case -- upper level protocol is IP */
  90.     if(at != NULLATYPE && arp.protocol == at->iptype){
  91.         fprintf(fp," prot IP");
  92.         is_ip = 1;
  93.     } else {
  94.         fprintf(fp," prot 0x%x prlen %u",arp.protocol,arp.pralen);
  95.     }
  96.     switch(arp.opcode){
  97.     case ARP_REQUEST:
  98.         fprintf(fp," op REQUEST");
  99.         break;
  100.     case ARP_REPLY:
  101.         fprintf(fp," op REPLY");
  102.         break;
  103.     case REVARP_REQUEST:
  104.         fprintf(fp," op REVERSE REQUEST");
  105.         break;
  106.     case REVARP_REPLY:
  107.         fprintf(fp," op REVERSE REPLY");
  108.         break;
  109.     default:
  110.         fprintf(fp," op %u",arp.opcode);
  111.         break;
  112.     }
  113.     fprintf(fp,"\n");
  114.     fprintf(fp,"sender");
  115.     if(is_ip)
  116.         fprintf(fp," IPaddr %s",inet_ntoa(arp.sprotaddr));
  117.     fprintf(fp," hwaddr %s\n",at->format(tmp,arp.shwaddr));
  118.  
  119.     fprintf(fp,"target");
  120.     if(is_ip)
  121.         fprintf(fp," IPaddr %s",inet_ntoa(arp.tprotaddr));
  122.     fprintf(fp," hwaddr %s\n",at->format(tmp,arp.thwaddr));
  123. }
  124.